package com.edu.modules.baseinfo.service;

import com.alibaba.fastjson.JSONObject;
import com.edu.modules.baseinfo.dao.LessonRecordDao;
import com.edu.modules.baseinfo.vo.ResultVo;
import com.edu.modules.entity.TLessonRecordEntity;
import com.sari.common.service.BaseService;
import com.sari.common.utils.DateUtils;
import com.sari.common.utils.FileUtils;
import com.sari.common.utils.IdGen;
import com.sari.common.utils.Reflections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kwj on 2016/7/15.
 */
@Service
public class LessonRecordService extends BaseService {

	@Autowired
	private LessonRecordDao lessonRecordDao;

	public ResultVo saveLessonRecord(JSONObject param){

		String msg = null;
		boolean flag = false;

		TLessonRecordEntity LessonRecord = new TLessonRecordEntity();
		param.put("lessonRecordId", IdGen.uuid());
		param.put("createTime", DateUtils.getDateTime());
		Reflections.initBean(LessonRecord,param);

		try {
			lessonRecordDao.saveObject(LessonRecord);
			msg = "success";
			flag = true;
		}catch (Exception e){
			msg = "fail";
			flag = true;
		}
		return new ResultVo(flag,msg);
	}

	public JSONObject doUploadVideo(MultipartFile file){

		JSONObject result= new JSONObject();
		if(!file.isEmpty()){
			try {
				String fileName = System.currentTimeMillis()+ file.getOriginalFilename();
				String filePath = "/opt/apache-tomcat-8.5.30/webapps/video";
				FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath,
						fileName));
				result.put("fileName",fileName);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public JSONObject getLessonRecordByPage(JSONObject param) {

		int offset = param.getInteger("offset");
		int limit = param.getInteger("limit");

		String lessonName = param.getString("lessonName");
		String lessonClass = param.getString("lessonClass");
		String teacherName = param.getString("teacherName");
		String subject = param.getString("subject");
		String startTime = param.getString("startTime");
		String endTime = param.getString("endTime");

		List list = new ArrayList();
		list = lessonRecordDao.getLessonRecordByPage(lessonName,lessonClass,
				teacherName,subject,startTime,endTime,offset,limit);

		JSONObject result= new JSONObject();
		result.put("total",lessonRecordDao.getLessonRecordCounts(lessonName,lessonClass,
				teacherName,subject,startTime,endTime));
		result.put("rows", list);

		return result;
	}

	public void updateLessonRecord(JSONObject param){

		TLessonRecordEntity objInfo = new TLessonRecordEntity();
		param.put("createTime", DateUtils.getDateTime());
		Reflections.initBean(objInfo,param);
		lessonRecordDao.updateObject(objInfo);
	}

	public ResultVo deleteLessonRecord(String lessonRecordId){

		lessonRecordDao.deleteObjects("t_lesson_record","lesson_record_id",lessonRecordId);
		return new ResultVo(true,"success");
	}
}
